<template>
  <div>
    <!-- 展示订单详情的结构 -->
    <el-card class="box-card">
      <!-- 卡片头部的结构 -->
      <template #header>
        <div class="detail">挂号详情</div>
      </template>
      <!-- 展示身体部分的顶部结构 -->
      <div class="top">
        <!-- 左侧标签 -->
        <el-tag type="success" effect="dark">
          <div class="tag">
            <svg
              t="1716359156151"
              class="icon"
              viewBox="0 0 1024 1024"
              version="1.1"
              xmlns="http://www.w3.org/2000/svg"
              p-id="2593"
              width="16"
              height="16"
            >
              <path
                d="M371.5904 862.528a47.232 47.232 0 0 1-30.72-9.92L13.1904 531.136a42.368 42.368 0 0 1 0-61.376 42.432 42.432 0 0 1 61.44 0l295.488 292.224 579.264-587.392a42.432 42.432 0 0 1 61.44 0 42.368 42.368 0 0 1 0 61.44l-608.512 613.632a40.576 40.576 0 0 1-30.72 12.864z"
                fill="#ffffff"
                p-id="2594"
              />
            </svg>
            <span>{{ orderInfo.param?.orderStatusString }}</span>
          </div>
        </el-tag>
        <div class="right_info">
          <img src="@/assets/images/code_app.png" />
          <div>
            <p>
              微信
              <svg
                t="1716360790320"
                class="icon"
                viewBox="0 0 1024 1024"
                version="1.1"
                xmlns="http://www.w3.org/2000/svg"
                p-id="5222"
                width="24"
                height="24"
              >
                <path
                  d="M683.058 364.695c11 0 22 1.016 32.943 1.976C686.564 230.064 538.896 128 370.681 128c-188.104 0.66-342.237 127.793-342.237 289.226 0 93.068 51.379 169.827 136.725 229.256L130.72 748.43l119.796-59.368c42.918 8.395 77.37 16.79 119.742 16.79 11 0 21.46-0.48 31.914-1.442a259.168 259.168 0 0 1-10.455-71.358c0.485-148.002 128.744-268.297 291.403-268.297l-0.06-0.06z m-184.113-91.992c25.99 0 42.913 16.79 42.913 42.575 0 25.188-16.923 42.579-42.913 42.579-25.45 0-51.38-16.85-51.38-42.58 0-25.784 25.93-42.574 51.38-42.574z m-239.544 85.154c-25.384 0-51.374-16.85-51.374-42.58 0-25.784 25.99-42.574 51.374-42.574 25.45 0 42.918 16.79 42.918 42.575 0 25.188-16.924 42.579-42.918 42.579z m736.155 271.655c0-135.647-136.725-246.527-290.983-246.527-162.655 0-290.918 110.88-290.918 246.527 0 136.128 128.263 246.587 290.918 246.587 33.972 0 68.423-8.395 102.818-16.85l93.809 50.973-25.93-84.677c68.907-51.93 120.286-119.815 120.286-196.033z m-385.275-42.58c-16.923 0-34.452-16.79-34.452-34.179 0-16.79 17.529-34.18 34.452-34.18 25.99 0 42.918 16.85 42.918 34.18 0 17.39-16.928 34.18-42.918 34.18z m188.165 0c-16.984 0-33.972-16.79-33.972-34.179 0-16.79 16.927-34.18 33.972-34.18 25.93 0 42.913 16.85 42.913 34.18 0 17.39-16.983 34.18-42.913 34.18z"
                  fill="#09BB07"
                  p-id="5223"
                />
              </svg>关注“北京114预约挂号”
            </p>
            <p>快速挂号，轻松就医</p>
          </div>
        </div>
      </div>
      <!-- 订单详情底部的结构 -->
      <div class="bottom">
        <!-- 左侧展示订单详情信息 -->
        <div class="left">
          <el-descriptions class="margin-top" :column="1" border>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">就诊人信息</div>
              </template>
              {{orderInfo.patientName}}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">就诊日期</div>
              </template>
              {{orderInfo.reserveDate}}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">就诊医院</div>
              </template>
              {{orderInfo.hosname}}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">就诊科室</div>
              </template>
              <el-tag size="small">{{ orderInfo.depname }}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">医生职称</div>
              </template>
              {{ orderInfo.title }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">医师服务费</div>
              </template>
              {{orderInfo.amount}}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">挂号订单</div>
              </template>
              {{orderInfo.outTradeNo}}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">挂号时间</div>
              </template>
              {{orderInfo.createTime}}
            </el-descriptions-item>
            <el-descriptions-item v-if="orderInfo.orderStatus==0||orderInfo.orderStatus==1">
              <template #label>
                <div class="cell-item">取消预约</div>
              </template>
              <el-popconfirm title="确定取消预约吗？" @confirm="cancel">
                <template #reference>
                  <el-button>取消预约</el-button>
                </template>
              </el-popconfirm>
              <el-button type="primary" v-if="orderInfo.orderStatus==0" @click="openDialog">支付</el-button>
            </el-descriptions-item>
          </el-descriptions>
        </div>
        <div class="right">
          <el-card style="max-width: 480px">
            <template #header>
              <div class="card-header">
                <span class="notice">注意事项</span>
              </div>
            </template>
            <p>1.请确认就诊人信息是否准确，若填写错误将无法取号就诊，损失由本人承担；</p>
            <p class="red">2.【取号】就诊当天需在{{orderInfo.fetchTime}}前在医院内取号，未取号视为过期，该号不退不换；</p>
            <p>3.【退号】在{{orderInfo.quitTime}}前可在线退号，逾期将不可办理退号退费；</p>
            <p>4.北京114预约挂号支持自费患者使用身份证预约，同时支持北京市医保患者使用北京社保卡在平台上预约挂号，请于就诊当日，携带预约画好所使用的有效身份证件到院取号；</p>
            <p>5.请注意北京市医保患者在住院期间不能使用社保卡在门诊取号。</p>
          </el-card>
        </div>
      </div>
    </el-card>
    <!-- 展示支付二维码的结构 -->
    <el-dialog @close="close" v-model="dialogVisible" title="微信支付" width="400">
      <!-- 支付需要使用的二维码图片 -->
      <img :src="imgUrl" />
      <p>请使用微信扫一扫</p>
      <p>扫描二维码支付</p>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="closeDialog">关闭窗口</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
  
<script lang="ts" setup>
// @ts-ignore
import {ref,onMounted} from "vue"
// @ts-ignore
import {reqOrderInfo,reqCancelOrder,reqQrcode,reqQueryPayState} from "@/api/user"
// @ts-ignore
import type {OrderResponseData, OrderInfo,Qrcode,PayResult} from "@/api/user/type"
import {useRoute} from "vue-router"
// @ts-ignore
import { ElMessage } from 'element-plus'
// @ts-ignore
// 生成二维码插件qrcode
import QRCode from 'qrcode'
let route=useRoute()
let orderInfo=ref<any>({})
// 定义存储二维码图片的路径
let imgUrl=ref<string>('')
// 存储定时器的使用
let timer=ref<any>()
const getOrderInfo=async()=>{
  let result:OrderResponseData=await reqOrderInfo(route.query.orderId as string)
  if(result.code==200){
    orderInfo.value=result.data
  }
}
onMounted(()=>{
  getOrderInfo()
})
// 取消订单
const cancel=async()=>{
  try{
    // 取消预约成功
    let result=await reqCancelOrder(route.query.orderId as string)
    console.log(result)
    // 再次获取订单详情的数据
    }catch(error){
      ElMessage({
        type:'error',
        message:'取消预约失败'
      })
    }
}
// 控制对话框的显示与隐藏
let dialogVisible=ref<boolean>(false)
// 点击支付按钮的回调
const openDialog=async()=>{
  let result:Qrcode=await reqQrcode(route.query.orderId as string)
  // 根据服务器返回的二维码信息生成二维码图片
  imgUrl.value=await QRCode.toDataURL(result.data.codeUrl)
  // 询问服务器当前这笔交易的支付结果
  // 只要二维码出来：需要每隔几秒询问服务器是否支付成功
  timer.value=setInterval(async()=>{
    let result:PayResult=await reqQueryPayState(route.query.orderId as string)
    // 如果服务器返回的数据data:true,代表支付成功
    if(result.data){
      dialogVisible.value=false;
      // 提示的信息
      ElMessage({
        type:'success',
        message:'支付成功'
      })
      // 清除定时器
      clearInterval(timer.value)
      // 再次获取订单详情 的数据
      getOrderInfo()
    }
  },2000)
  // 展示对话框
  dialogVisible.value=true
}
// 关闭对话框
const closeDialog=()=>{
  dialogVisible.value=false;
  clearInterval(timer.value)
}
// 对话框右上角关闭
const close=()=>{
  clearInterval(timer.value)
}
</script>
  
<style lang="scss" scoped>
.box-card {
  cursor: pointer;
  .detail {
    letter-spacing: 1px;
    font-weight: 700;
    color: #333;
    font-size: 16px;
  }
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #eee;
    padding-bottom: 15px;
    .tag {
      display: flex;
      align-items: center;
      span {
        font-size: 16px;
        letter-spacing: 1px;
        margin-left: 5px;
      }
    }
    .right_info {
      display: flex;
      align-items: center;
      img {
        width: 61px;
        height: 61px;
        margin-right: 12px;
      }
      p {
        display: flex;
        color: #999;
        font-size: 16px;
        line-height: 22px;
      }
    }
  }
  .bottom {
    display: flex;
    justify-content: space-between;
    margin-top: 40px;
    .left {
      flex: 4;
    }
    .right {
      flex: 4;
      margin-left: 20px;
      .notice {
        color: #333;
        font-weight: 700;
        font-size: 14px;
        letter-spacing: 1px;
      }
      p {
        color: #666;
        letter-spacing: 1px;
        line-height: 26px;
        font-size: 14px;
        margin-bottom: 10px;
      }
      .red {
        color: red;
      }
    }
  }
}
::v-deep(.el-descriptions__body
    .el-descriptions__table.is-bordered
    .el-descriptions__cell) {
  width: 50px;
  text-align: center;
}
::v-deep(.el-dialog__body) {
  display: flex;
  flex-direction: column;
  align-items: center;
  img {
    margin-bottom: 15px;
  }
  p {
    line-height: 20px;
    letter-spacing: 1px;
  }
}
</style>
